package com.winexpress.product;

import com.winexpress.util.Log;
import com.winexpress.util.SessionAttributes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "CategoryManagement", urlPatterns = {"/categoryManagement/*"})
public class CategoryManagement extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WineXpress-war/ProductManagementWS.wsdl")
    private ProductManagementWS_Service service;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try {
            RequestDispatcher dispatcher;
            ServletContext servletContext = getServletContext();
            HttpSession session = request.getSession(true);
            String page = request.getPathInfo().substring(1);
            ProductManagementWS port = service.getProductManagementWSPort();
             if (session.getAttribute(SessionAttributes.USER_ID) == null || session.getAttribute(SessionAttributes.IS_ADMIN) == null ||session.getAttribute(SessionAttributes.IS_ADMIN).toString().equals("false")) {
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                 System.out.println(session.getAttribute(SessionAttributes.IS_ADMIN));
                return;
            }      
            
            //redirect to manageCategory.jsp
            if ("manageCategory".equals(page)) {

                //add category
                if ("addCategory".equals(request.getParameter("add"))) {
                    Category newCategory = new Category();
                    String category = request.getParameter("newCategory");
                    newCategory.setName(category);
                    Boolean isAdd = port.addCategory(newCategory);
                    request.setAttribute("isAdd", isAdd);
                    Log.info("adminID = " + SessionAttributes.USER_ID  + "add wine "+category);
                }

                //delete category  
                if ("delete".equals(request.getParameter("delete"))) {
                    String deleteID = request.getParameter("deleteID");
                    Boolean isDelete = port.deleteCategory(new Long(deleteID));
                    request.setAttribute("isDelete", isDelete);
                    Log.info("adminID = " + SessionAttributes.USER_ID  + "delete wine "+deleteID);
                }

                //edit category  
                if ("edit".equals(request.getParameter("edit"))) {
                    Long id = new Long(request.getParameter("editID")).longValue();
                    Category editedCategory = port.getCategory(id);
                    editedCategory.setName(request.getParameter("editName"));
                    Boolean isEdit = port.editCategory(editedCategory);
                    request.setAttribute("isEdit", isEdit);
                    Log.info("adminID = " + SessionAttributes.USER_ID  + "edit wine "+id.toString());
                }
                
                List<Category> list = port.listCategory();
                session.setAttribute("categoryList", list);
            }

            dispatcher = servletContext.getNamedDispatcher(page);
            if (dispatcher == null) {
                dispatcher = servletContext.getNamedDispatcher("Error");
            }
            dispatcher.forward(request, response);
        } catch (Exception ex) {
            log("Exception in Category Management servlet");
            log(ex.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
